/*----------------------------------------------------------------------------------*/
/*!
 * @file enemy.hxx
 * @brief Source-File der Enemy Informationen
 * @author steff
 *
 */
/*------------------------------------------------------------------------------------*/

#include <stdio.h>
#include "enemy.hxx"

unsigned int sei_create_collection(char * filename)
{
	unsigned int ret = 0;
	struct sei_header header;
	struct enemy_object_info first_enemy;
	struct enemy_object_info second_enemy;
	FILE *target_handler;
	
	header.id[0] = 'S';
	header.id[1] = 'E';
	header.id[2] = 'I';
	header.id[3] = 0;
	header.id[4] = 0;
	header.version = 1;

	sprintf(first_enemy.name, "enemy1");
	sprintf(first_enemy.animation_data, "fighter1.png");
	sprintf(first_enemy.sprite_size_w, "50");
	sprintf(first_enemy.sprite_size_h, "80");
	sprintf(first_enemy.life_points, "2");
	sprintf(first_enemy.maximum_speed, "50");
	sprintf(first_enemy.std_sprite_from, "0");
	sprintf(first_enemy.std_sprite_to, "0");
	sprintf(first_enemy.fir_sprite_from, "0");
	sprintf(first_enemy.fir_sprite_to, "0");
	sprintf(first_enemy.exp_sprite_from, "1");
	sprintf(first_enemy.exp_sprite_to, "3");
	sprintf(first_enemy.reserved, "\0");


	sprintf(second_enemy.name, "enemy2");
	sprintf(second_enemy.animation_data, "other.png");
	sprintf(second_enemy.sprite_size_w, "70");
	sprintf(second_enemy.sprite_size_h, "120");
	sprintf(second_enemy.life_points, "2");
	sprintf(second_enemy.maximum_speed, "50");
	sprintf(second_enemy.std_sprite_from, "0");
	sprintf(second_enemy.std_sprite_to, "0");
	sprintf(second_enemy.fir_sprite_from, "0");
	sprintf(second_enemy.fir_sprite_to, "0");
	sprintf(second_enemy.exp_sprite_from, "1");
	sprintf(second_enemy.exp_sprite_to, "3");
	sprintf(second_enemy.reserved, "\0");

	target_handler = fopen(filename, "w");
	if (target_handler != NULL)
	{
		ret = 1;
		fwrite(&header, sizeof(header), 1, target_handler);
		fwrite(&first_enemy, sizeof(first_enemy), 1, target_handler);
		fwrite(&second_enemy, sizeof(second_enemy), 1, target_handler);
		fclose(target_handler);
	}
	
	return ret;
}

/* check if a file is a valid sei collection	*/
/* return 1 if valid							*/
unsigned int sei_check_file(char * filename)
{
	unsigned int ret = 0;
	FILE * sei_collection;
	struct sei_header header;

	sei_collection = fopen(filename, "r");
	if (sei_collection != NULL)
	{
		fread(&header, sizeof(header), 1, sei_collection);
		if ((header.id[0] == 'S') && (header.id[1] == 'E') && (header.id[2] == 'I')) ret = 1;
		fclose(sei_collection);
	}

	return ret;
}

/* load an enemy collection */
enemy_collection_object * sei_load_collection(char * filename)
{
	enemy_collection_object * real_anchor;
	enemy_collection_object * temp_ptr;
	struct enemy_object_info temp_inf;
	struct sei_header header;
	FILE * file_handler;
	long file_size;
	if (sei_check_file(filename))
	{
		file_handler = fopen(filename, "r");
		fseek(file_handler, 0, SEEK_END);
		file_size = ftell(file_handler);
		rewind(file_handler);
		fread(&header, sizeof(header), 1, file_handler);
		
		if (!feof(file_handler))
		{
			fread(&temp_inf, sizeof(temp_inf), 1, file_handler);
			real_anchor = new enemy_collection_object(temp_inf);
			temp_ptr = real_anchor;
		
			while(ftell(file_handler) < file_size)
			{
				fread(&temp_inf, sizeof(temp_inf), 1, file_handler);
				temp_ptr->next = new enemy_collection_object(temp_inf);
				temp_ptr = temp_ptr->next;
			}
		} else
		{
			real_anchor = NULL;
		}
		fclose(file_handler);
	}
	return real_anchor;
}

/* save an enemy collection */
unsigned int sei_save_collection(char * filename, enemy_collection_object * anchor)
{
	unsigned int ret = 0;
	struct sei_header header;
	FILE * target_handler;
	enemy_collection_object * temp_ptr = anchor;
	header.id[0] = 'S';
	header.id[1] = 'E';
	header.id[2] = 'I';
	header.id[3] = 0;
	header.id[4] = 0;
	header.version = 1;
	target_handler = fopen(filename, "w");
	if (target_handler != NULL)
	{
		ret = 1;
		fwrite(&header, sizeof(header), 1, target_handler);
		while(temp_ptr != NULL)
		{
			fwrite(&temp_ptr->data, sizeof(enemy_object_info), 1, target_handler);
			temp_ptr = temp_ptr->next;
		}
		fclose(target_handler);
	}
	return ret;
}

/* count enemy objects */
unsigned int sei_count_enemies(enemy_collection_object * anchor)
{
	unsigned int ret = 0;
	enemy_collection_object * temp_ptr = anchor;
	while(temp_ptr != NULL)
	{
		ret++;
		temp_ptr = temp_ptr->next;
	}
	return ret;
}

enemy_collection_object :: enemy_collection_object(struct enemy_object_info fill_with)
{
	data = fill_with;
	next = NULL;
}

enemy_collection_object :: ~enemy_collection_object(void)
{
}


